package rollercoaster.railway.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import rollercoaster.commons.Vertex3D;
import rollercoaster.commons.interfaces.IVertexReader;

/* 
 * Since the defined interface doesn't allow me to specify anything as a parameter of the getVertices object,
 * I exploit polymorphism being a Java expert, defining a couple of methods and a constructor 
 * to dodge the problem LIKE A BOSS.
*/

public class OBJFileVertexReader implements IVertexReader{
	
	private String filepath;
	

	public OBJFileVertexReader() {
	}
	
	public OBJFileVertexReader(String filepath) {
		this.filepath = filepath;
	}

	public List<Vertex3D> getVertices(String filepath){
		this.filepath = filepath;
		return getVertices();
	}

	public List<Vertex3D> getVertices() {
		try {
			BufferedReader reader = new BufferedReader(new FileReader(new File(filepath)));
			ArrayList<Vertex3D> list = new ArrayList<Vertex3D>();
			String line;
			String[] vertex;
			
			//Skip bullshit in obj file and reading things that matter
			while((line = reader.readLine()) != null){
				if(line.startsWith("v ")){
					vertex = line.split(" ");
					list.add(new Vertex3D(-Float.parseFloat(vertex[1]), Float.parseFloat(vertex[2]), Float.parseFloat(vertex[3])));					
				}

			}
			
			reader.close();
			return list;
		
		} catch (FileNotFoundException e) {
			System.out.println("File not Found");
			e.printStackTrace();
			return null;
		} catch (IOException e) {
			System.out.println("Error while reading File");
			e.printStackTrace();
			return null;
		}
	}
	

}
